package com.lanchetech.merchant.controller;

import com.lanchetech.bean.vo.MerchantDetailVO;
import com.lanchetech.entity.*;
import com.lanchetech.merchant.aop.OperatorLog;
import com.lanchetech.merchant.service.MerchantService;
import com.lanchetech.bean.request.MerchantRolePermissionReq;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.request.SysLogReq;
import com.lanchetech.bean.request.UpdatePasswordReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "管理员信息接口", tags = "管理员信息接口")
@RequestMapping("/api/merchant")
@RestController
public class MerchantController {

    @Autowired
    MerchantService merchantService;


    @ApiOperation("获取管理员详细信息")
    @GetMapping("/getMerchantDetail")
    public ResultData<MerchantDetailVO> getMerchantDetail(@AuthenticationPrincipal Merchant merchant) {
        return merchantService.getMerchantDetail(merchant);
    }

    @ApiOperation("获取管理员权限列表")
    @GetMapping("/getMerchantPermissions")
    public ResultData<List<MerchantPermission>> getMerchantPermissions(@AuthenticationPrincipal Merchant merchant) {
        return merchantService.getMerchantPermissions(merchant);
    }

    @ApiOperation("管理员修改密码")
    @PostMapping("/updatePassword")
    public BaseResp updatePassword(@AuthenticationPrincipal Merchant merchant, @RequestBody UpdatePasswordReq req) {
        return merchantService.updatePassword(merchant, req);
    }

    @ApiOperation("获取管理员列表")
    @PreAuthorize("hasAuthority('merchant.index')")
    @PostMapping("/getMerchantPage")
    public ResultData<BasePageResp<Merchant>> getMerchantPage(@RequestBody PageReq req) {
        return merchantService.getMerchantPage(req);
    }

    @ApiOperation("获取管理员角色列表")
    @PreAuthorize("hasAuthority('merchant.role')")
    @GetMapping("/getMerchantRoleList")
    public ResultData<List<MerchantRole>> getMerchantRoleList() {
        return merchantService.getMerchantRoleList();
    }

    @ApiOperation("获取管理员角色分页")
    @PreAuthorize("hasAuthority('merchant.role')")
    @PostMapping("/getMerchantRolePage")
    public ResultData<BasePageResp<MerchantRole>> getMerchantRolePage(@RequestBody PageReq req) {
        return merchantService.getMerchantRolePage(req);
    }

    @ApiOperation("获取管理员角色拥有的权限列表")
    @PreAuthorize("hasAuthority('merchant.permission')")
    @GetMapping("/getMerchantRolePermissionList")
    public ResultData<List<MerchantPermission>> getMerchantRolePermissionList(@RequestParam("id") Long id) {
        return merchantService.getMerchantRolePermissionList(id);
    }

    @ApiOperation("获取管理员所有权限列表")
    @PreAuthorize("hasAuthority('merchant.permission')")
    @GetMapping("/getAllPermissionList")
    public ResultData<List<MerchantPermission>> getAllPermissionList() {
        return merchantService.getAllPermissionList();
    }

    @OperatorLog("更新管理员角色的权限")
    @ApiOperation("更新管理员角色的权限")
    @PreAuthorize("hasAuthority('merchant.role.permission.auth')")
    @PostMapping("/updateMerchantRolePermission")
    public BaseResp updateMerchantRolePermission(@RequestBody MerchantRolePermissionReq req) {
        return merchantService.updateMerchantRolePermission(req);
    }

    @OperatorLog("添加或修改管理员角色")
    @ApiOperation("添加或修改管理员角色")
    @PreAuthorize("hasAuthority('merchant.role.edit.auth')")
    @PostMapping("/editMerchantRole")
    public BaseResp editMerchantRole(@RequestBody MerchantRole merchantRole) {
        return merchantService.editMerchantRole(merchantRole);
    }

    @OperatorLog("删除管理员角色")
    @ApiOperation("删除管理员角色")
    @PreAuthorize("hasAuthority('merchant.role.edit.auth')")
    @GetMapping("/deleteMerchantRole")
    public BaseResp deleteMerchantRole(@RequestParam("id") Long id) {
        return merchantService.deleteMerchantRole(id);
    }

    @OperatorLog("添加或修改管理员")
    @ApiOperation("添加或修改管理员")
    @PreAuthorize("hasAuthority('merchant.index.edit.auth')")
    @PostMapping("/editMerchant")
    public BaseResp editMerchant(@RequestBody Merchant req, @AuthenticationPrincipal Merchant merchant) {
        return merchantService.editMerchant(merchant, req);
    }

    @OperatorLog("删除管理员")
    @ApiOperation("删除管理员")
    @PreAuthorize("hasAuthority('merchant.index.edit.auth')")
    @GetMapping("/deleteMerchant")
    public BaseResp deleteMerchant(@RequestParam("id") Long id, @AuthenticationPrincipal Merchant merchant) {
        return merchantService.deleteMerchant(merchant, id);
    }

    @OperatorLog("重置管理员密码")
    @ApiOperation("重置管理员密码")
    @PreAuthorize("hasAuthority('merchant.index.edit.auth')")
    @GetMapping("/resetMerchantPassword")
    public BaseResp resetMerchantPassword(@RequestParam("id") Long id) {
        return merchantService.resetMerchantPassword(id);
    }

    @ApiOperation("查询指定管理员详细信息")
    @PreAuthorize("hasAuthority('merchant.edit')")
    @GetMapping("/queryMerchantDetail")
    public ResultData<Merchant> queryMerchantDetail(@RequestParam("id") Long id) {
        return merchantService.queryMerchantDetail(id);
    }

    @ApiOperation("获取管理员操作日志分页")
    @PreAuthorize("hasAuthority('merchant.sys-log')")
    @PostMapping("/getSysLogPage")
    public ResultData<BasePageResp<MerchantSysLog>> getSysLogPage(@RequestBody SysLogReq req) {
        return merchantService.getSysLogPage(req);
    }

}
